<template>
	<view class="visit-container">
		<view class="service-category">
			<u-subsection :list="categoryList" keyName="name" :current="curNow" @change="sectionChange"></u-subsection>
		</view>
		<view class="visit-today">
			<view class="title">
				今日访客
			</view>

			<view class="info">
				<uni-table border stripe emptyText="暂无更多数据">
					<uni-tr>
						<uni-th width="50" align="center">序号</uni-th>
						<uni-th width="80" align="center">姓名</uni-th>
						<uni-th width="80" align="center">状态</uni-th>
						<uni-th width="80" align="center">操作</uni-th>
					</uni-tr>
					<uni-tr v-for="(item, index) in visitTodayList" :key="index">
						<uni-td align="center">{{ index+1 }}</uni-td>
						<uni-td align="center">
							<view class="name">{{ item.intervieweeName }}</view>
						</uni-td>

						<!-- <uni-td align="center">{{ item.tates===1?'已签到':'未签到' }}</uni-td> -->
						<!-- <uni-td align="center"><u-button text="修改"></u-button></uni-td> -->
						<uni-td align="center">
							<u-button :text="item.state==1?'待访问':(item.state==2?'待审核':(item.state==3?'已完成':'已失效'))"
								:type="item.state==1?'success':(item.state==2?'warning':(item.state==3?'info':'error'))"
								size="mini">
							</u-button>
						</uni-td>
						<uni-td align="center">
							<u-button text="详情" size="mini" @click="toDetail(item.id)"></u-button>
						</uni-td>
					</uni-tr>
				</uni-table>

			</view>

		</view>

		<view class="visit-all">
			<view class=" title">
				<u-text text="所有访客"></u-text>
			</view>
			<view class="title">
				<u-list>
					<u-list-item v-for="(item, index) in visitList" :key="index">
						<u-cell size="large" :title="item.intervieweeName"
							:value="item.state==1?'待访问':(item.state==2?'待审核':(item.state==3?'已完成':'已失效'))"
							:label="item.schoolTime" isLink
							@click="toDetail(item.id)">
						</u-cell>
					</u-list-item>
				</u-list>
			</view>
		</view>
		<uni-fab ref="fab" horizontal="right" vertical="bottom" direction="horizontal" @fabClick="toAddVisitor()"/>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				categoryList: [{
						id: 0,
						name: '全部'
					},
					{
						id: 1,
						name: '待访问'
					},
					{
						id: 2,
						name: '待审核'
					},
					{
						id: 3,
						name: '已完成'
					},
					{
						id: 4,
						name: '已失效'
					},
				],
				curNow: 0,
				visitSoureList: [],
				visitList: [],
				visitTodayList: [],

			};
		},
		onLoad() {
			this.getVisitList()
			let _this=this
			uni.$on('onVisitor',function(data){
					console.log('监听到事件来自 onVisitor ，携带参数 msg 为：' + data.msg+' 状态码为 '+data.code);
				_this.getVisitList()
					// this.visitSoureList.push(data)
				})
				uni.$on('change',function(data){
					var index = _this.visitList.findIndex(item => {
					  return item.id === data.id;
					})
					_this.visitList[index].state=data.state
					console.log('index',index)
				})
		},
		onShow() {
				console.log('onShow')
		},
		onHide() {
			console.log('onHide')
		},
		methods: {
			toDetail(id){
				uni.navigateTo({
					url:'/pages/vistor/visitorDetail?id='+id
				})
			},
			toAddVisitor(){
				uni.navigateTo({
					url:"/pages/vistor/addVisitor"
				})
			},
			sectionChange(index) {
				this.curNow = index;
				if (index === 0) {
					this.visitList = this.visitSoureList
					// this.visitTodayList=this.visitSoureList.filter(item=>{
					// 	item.schoolTime===this.getNowDate()
					// })
				} else {
					this.visitList = this.visitSoureList.filter(item => {
						return item.state === index
					})
					// this.visitTodayList=this.visitSoureList.filter(item=>{
					// 	return item.state===index && item.schoolTime===this.getNowDate()
					// })
				}
			},
			// getNowDate() {
			// 	let nowDate = new Date()
			// 	let year = nowDate.getFullYear()
			// 	var month = nowDate.getMonth() + 1
			// 	var day = nowDate.getDate()

			// 	if (month < 10) {
			// 		month = '0' + month
			// 	}
			// 	if (month < 10) {
			// 		day = '0' + day
			// 	}


			// 	return year + '-' + month + '-' + day
			// },
			getVisitList(index = 0) {

				uni.request({
					url: 'https://www.gzzyue.com/prod-api/system/visitToday/list',
					method: 'GET',
					header: {
						'Authorization': uni.getStorageSync("token") //自定义请求头信息
					},
					success: (res) => {
						console.log('getVisitList:', res);
						console.log('date:', this.$nowDate());
						if (res.statusCode == 200) {
							if (res.data.code == 200) {
								console.log(new Date().getFullYear(), new Date().getMonth() + 1, new Date().getDate())
								this.visitSoureList =res.data.rows
								this.visitList = res.data.rows
								this.visitTodayList = this.visitList.filter(item => {
									// return item.ysstate===1
									// return item.schoolTime =new Date().getFullYear()+'-'+new Date().getMonth()+1+'-'+new Date().getDate()
									return item.schoolTime === this.$nowDate()
								})
							} else if (res.data.code == 500) {
								uni.$u.toast(res.data.msg)
							} else {
								uni.$u.toast('系统服务异常，请联系管理员！')
							}
						} else {
							uni.$u.toast('系统服务异常，请联系管理员！')
						}
					},
					fail: (errors) => {
						console.log('errors:', errors);
						uni.$u.toast('服务器错误，请联系管理员！')
					},
				});
			},
		}
	}
</script>

<style lang="scss">

</style>
